Omplus.views.ManagementPanel = Omplus.base.extend(Omplus.views.Panel, {
    init:function () {
    	var _me = this;
        this.width = this.pixelsWidth + "px";
        this.layout.width = this.pixelsWidth + "px";
        this.tabProto.renderTo = this.omplusName;
        this.fieldProto.model = this.models.search;
        this.fieldProto.controller = this.controller;
        if (this.views.search && this.views.search.length) {
            this.searchProto = jQuery.extend(true, {}, this.tabProto);
            var horizontal = {
                omplusType:"Panel",
                components:[],
                width:Math.ceil((this.pixelsWidth - 300) / 2) + "px"
            };
            this.searchProto.omplusName = this.omplusName + "_searchTab";
            this.searchProto.model = "Assets.list.label_search_button.localValue";
            this.searchProto.controller = "languageController";
            this.searchProto.components = new Array();
            var fieldContainer = {
                omplusName : this.omplusName + "_fieldContainer",
                omplusType:"Panel",
                components : new Array(),
                cssClasses:["fieldContainer"]
            }
            for (var i = 0; i < 3; i++) {
                horizontal.omplusName = this.omplusName + "_panelLayout_" + i;
                fieldContainer.components.push(jQuery.extend(true, {}, horizontal));
                fieldContainer.components[i].omplusName = this.omplusName + "_search_" + i;
            }
            this.searchProto.components.push(fieldContainer);
            var columns = Math.ceil(this.views.search.length / 2);
            for (var i in this.views.search) {
                var helper = jQuery.extend(true, {}, this.fieldProto);
                helper = jQuery.extend(true, helper, this.views.search[i]);
                helper.omplusName = this.omplusName + "_field_" + i;
                if (this.views.search[i].inputType)
                    helper.inputType = this.views.search[i].inputType;
                if (!helper.controller)
                    helper.controller = this.controller;
                helper.labelModel = this.views.search[i].labelModel;
                var panel = this;
                if (helper.perPage) {
                    helper.omplusName = this.omplusName + "_perPage";
                    var helperNew = helper;
                    helper.select = function () {
                        Omplus.globals.getObject(panel.omplusName + "_tableResult").changePerPage(Omplus.globals.getObject("PerPages." + helperNew.current +".number").getValue());
                        Omplus.globals.getObject(_me.omplusName + "_tableResult").onPageChanged(1);
                    }
                }
                if (helper.searchButton) {
                    helper.omplusName = this.omplusName + "_searchButton";
                    helper.click = function () {
                        Omplus.globals.getObject(_me.omplusName + "_tableResult").onPageChanged(1);
                    }
                }
                fieldContainer.components[Math.floor(i / columns)].components.push(helper);
            }
            if (this.views.searchResult && this.views.searchResult.length) {
                this.tableProto.omplusName = this.omplusName + "_table";
                this.tableProto.components[0].omplusName = this.omplusName + "_tableColumns";
                var tableRowProto = {
                    omplusType:this.omplusName + "_rowProto",
                    components:[]
                };
                for (var i in this.views.searchResult) {
                    if (!this.views.searchResult[i].getDetails) {
                        var label = jQuery.extend(true, {}, {
                            omplusType:"Label",
                            omplusName:this.omplusName + "_tableColumns_" + this.views.searchResult[i].model,
                            model:this.views.searchResult[i].labelModel,
                            controller:"languageController",
                            width:this.views.searchResult[i].width,
                            render:function () {
                                this.getContent().css({"margin-top":"20px"})
                            }
                        });
                        this.tableProto.components[0].components.push(label);
                        var column = jQuery.extend(true, {}, {
                            omplusType:this.views.searchResult[i].omplusType,
                            omplusName:this.omplusName + "_tableRow_" + i,
                            model:this.views.searchResult[i].model,
                            width:this.views.searchResult[i].width
                        });
                        if (this.views.searchResult[i].controller){
                            column.controller = this.views.searchResult[i].controller;
                        }
                        if (this.views.searchResult[i].click){
                            column.click = this.views.searchResult[i].click;
                        }
                        tableRowProto.components.push(column);
                        this.tableProto.components[1].rowCellWidths.push(this.views.searchResult[i].width);
                    }else{
                        var panel = this;
                        var column = jQuery.extend(true, {}, {
                            omplusType:this.views.searchResult[i].omplusType,
                            omplusName:this.omplusName + "_tableRow_" + i,
                            model:this.views.searchResult[i].model,
                            controller: this.views.searchResult[i].controller,
                            width:this.views.searchResult[i].width,
                            click:function () {
                                var array = $(this).closest(".tableRow").attr("id").split("_");
                                var row = array[array.length - 1];
                                var action = panel.actions[1];
                                _omplusConstants.opened_button = $(this);
                                _omplusConstants.opened_button.hide();
                                action.requestModel = helper.models.searchResult + "." + helper.models.searchResultList + ".member." + row;
                                action.run();
                            }
                        });
                        tableRowProto.components.push(column);
                        this.tableProto.components[1].rowCellWidths.push(this.views.searchResult[i].width);
                    }
                }
                Omplus.views[tableRowProto.omplusType] = Omplus.base.extend(Omplus.views.TableRow, tableRowProto);
                this.tableProto.components[1].omplusName = this.omplusName + "_tableResult";
                this.tableProto.components[1].controller = this.controller;
                this.tableProto.components[1].definition = {
                    ownerOfAction : this.omplusName,
                    nameOfAction : "searchResult",
                    list : this.models.searchResultList,
                    modelSearch : this.models.search
                };
                this.tableProto.components[1].omplusProto = this.omplusName + "_rowProto";
                this.tableProto.components[1].model = this.models.searchResult;
                this.searchProto.components.push(this.tableProto);
            }
            Omplus.base.create(Omplus.views.Panel, {
                omplusName:this.omplusName + "_tableOverlay",
                renderTo:this.omplusName + "_searchTab",
                width:Math.ceil(this.pixelsWidth - 50) + "px",
                showEffect:{
                    name:"fade",
                    time:1
                },
                hideEffect:{
                    name:"fade",
                    time:1
                }
            });
            var array = new Array();
            array.push(this.searchProto);
            for (var i in this.components) {
                array.push(this.components[i]);
            }
            this.components = array;
        }
        if (this.views.chooser){
            this.chooserProto.omplusName = this.omplusName + "_details";
            this.chooserProto.model = this.views.chooser.title;
            for (var i in this.views.chooser.components){
                this.views.chooser.components[i].management = this.omplusName;}
            this.chooserProto.components = this.views.chooser.components;
            Omplus.base.create(Omplus.views.TabPanel, this.chooserProto);
        }
        if (this.models.search){
            for (var i in this.actionsProto) {
                switch (this.actionsProto[i].omplusName) {
                    case "searchResult":
                        this.actionsProto[i].requestModel = this.models.search;
                        this.actionsProto[i].responseModel = this.models.searchResult;
                        var helper = this;
                        this.actionsProto[i].start = function () {
                            Omplus.globals.getObject(helper.omplusName + "_tableOverlay").show();
                        };
                        this.actionsProto[i].callback = function () {
                            Omplus.globals.getObject(helper.omplusName + "_tableOverlay").hide();
                        };
                        break;
                    case "getDetails":
                        this.actionsProto[i].responseModel = this.models.chooser + ".current";
                        var helper = this;
                        this.actionsProto[i].callback = function () {
                            var modelForDisplay = helper.models.forDisplay;
                            var current = Omplus.globals.getObject(helper.models.chooser + ".current");
                            var players = Omplus.globals.getObject(helper.models.chooser);
                            players.addToList(current.getValue(), true, modelForDisplay);
                            helper.actions[2].run();
                            _omplusConstants.opened_button.show();
                            if (Omplus.globals.getObject(helper.models.chooser + ".list").getValue().length == 1){
                                helper.append(helper.omplusName + "_details");
                            }
                            Omplus.globals.getObject(helper.omplusName + ".Layout").activate(-1);
                            // mark the newly opened tab
                            $("#" + helper.omplusName + "_details").find(".tabListContainer .tabListItem").removeClass("active");
                            var newTab = true;
                            $("#" + helper.omplusName + "_details").find(".tabListContainer .tabListItem").each(function () {
                                $(this).removeClass("active");
                                if ($(this).html() == current.getValue()[modelForDisplay]) {
                                    $(this).addClass("active");
                                    newTab = false;
                                }
                            });
                            if (newTab) {
                                $("#" +helper.omplusName + "_details").find(".tabListContainer .tabListItem:last").addClass("active");
                            }
                            $("#" + helper.omplusName + "_details_title").click();
                        };
                        break;
                    case "setInStorage":
                    case "getFromStorage":
                        this.actionsProto[i].responseModel = this.models.chooser;
                        this.actionsProto[i].proxy.key = this.omplusName + ".result";
                        break;
                }
                this.actionsProto[i].omplusName = this.omplusName + "." + this.actionsProto[i].omplusName;
                this.actions.push(Omplus.base.create(Omplus.actions[this.actionsProto[i].omplusType], this.actionsProto[i]));
            }
        }
    },
    onRenderStart:function () {
        this.actions[3].run();
    },
    showEffect:{
        name:"fade",
        time:300
    },
    hideEffect:{
        name:"fade",
        time:300
    },
    layout:{
        omplusType:"TabLayout"
    },
    height:"auto",
    pixelsWidth:1000,
    omplusType:"ManagementPanel",
    controller:null,
    models:{
        chooser:null,
        search:null,
        searchResult:null,
        searchResultList:null,
        forDisplay: null
    },
    views:{
        chooser : {
            title : null,
            components:[]
        },
        search:[],
        searchResult:[]
    },
    actionsProto:[
        {
            omplusType:"Get",
            omplusName:"searchResult",
            proxy:{
                omplusType:"AjaxProxy"
            }
        },
        {
            omplusType:"Get",
            omplusName:"getDetails",
            proxy:{
                omplusType:"AjaxProxy"
            }
        },
        {
            omplusType:"Set",
            omplusName:"setInStorage",
            proxy:{
                omplusType:"LocalStorage"
            },
            requestModel:"UserShort"
        },
        {
            omplusType:"Get",
            omplusName:"getFromStorage",
            proxy:{
                omplusType:"LocalStorage"
            },
            requestModel:"UserShort"
        }
    ],
    tabProto:{
        omplusType:"TabPanel",
        layout:{
            omplusType:"HorizontalLayout"
        },
        controller:"languageController",
        model:"Assets.list.players_label.localValue"
    },
    fieldProto:{
        omplusType:"LabelInputPair",
        labelController:"languageController",
        width:"100%",
        height:"50px"
    },
    chooserProto:{
        omplusType:"TabPanel",
        showEffect:{
            name:"slide",
            time:300,
            options:{
                direction:"up"
            }
        },
        hideEffect:{
            name:"slide",
            time:300,
            options:{
                direction:"up"
            }
        },
        layout:{
            omplusType:"HorizontalLayout"
        },
        controller:"languageController"
    },
    tableProto:{
        omplusType:"Panel",
        components:[
            {
                omplusName:"tableColumnNameLabels",
                omplusType:"Panel",
                components:[],
                cssClasses:[
                    "table-header"
                ]
            },
            {
                omplusType:"DinamicTable",
                orientation:"vertical",
                rowsPerPage:5,
                rowHeight:"35px",
                rowCellWidths:[]
            }
        ]
    },
    actions:[],
    onRenderFinish:function () {
        if (this.models.chooser != null && this.views.chooser != null)
            if (Omplus.globals.getObject(this.models.chooser + ".list").getValue().length){
                this.append(this.omplusName + "_details");
                Omplus.globals.getObject(this.models.chooser).chooseLast();
                $("#" +this.omplusName + "_details").find(".tabListContainer .tabListItem:last").addClass("active");
            }
    },
    save : function(){
        Omplus.globals.getObject(this.models.chooser).saveCurrent();
        this.actions[2].run();
    },
    refreshView : function(){
        var number = Omplus.globals.getObject(this.models.chooser + ".list").getValue().length;
        if (!number){
            Omplus.globals.getObject(this.omplusName + ".Layout").remove(-1);
        }else{
            Omplus.globals.getObject(this.models.chooser).chooseByIndex(0);
        }
    },
    getAction:function (actionName) {
        return Omplus.globals.getObject(this.omplusName + "." + actionName);
    }
});

Omplus.views.OpeningTabView = Omplus.base.extend(Omplus.views.Panel, {
   omplusType:"OpeningTabView",
   init : function(){
       this.reduce(this);
   },
    height: "auto",
   reduce : function(helper){
       for (var i in helper.components){
           if (!helper.components[i].omplusType){
               helper.components[i].omplusType = "LabelInputPair";
               helper.components[i].omplusName = helper.omplusName + "_field_" + i;
               helper.components[i].controller = this.controller;
               helper.components[i].labelController = "languageController";
               helper.components[i].width = "300px";
               if (!helper.components[i].height)
                    helper.components[i].height = "50px";
           }else if (helper.components[i].components){
               this.reduce(helper.components[i]);
           }
       }
   }
});

Omplus.views.NavbarButton = Omplus.base.extend(Omplus.views.Panel, {
    omplusType:"NavbarButton",
    init : function(){
        this.components[0].omplusName = this.omplusName + "_mainButton";
        this.width = this.bigWidth;
        this.height = "auto";
        this.components[0].width = this.bigWidth;
        this.components[0].height = this.bigHeight;
        this.components[1].width = this.bigWidth;
        this.components[1].height = "auto";
        this.components[1].omplusName = this.omplusName + "_smallButtons";
        var options = {
            omplusType:"Button",
            cssClasses:["big-button"],
            omplusName:this.omplusName + "_big",
            width: this.bigWidth,
            height: this.bigHeight,
            controller : "languageController",
            model : this.mainButton.model
        };
        if (this.mainButton["management"] && this.mainButton.management != null){
            var helper = this.mainButton.management;
            options.click = function(){
                $("#navbar .big-button button.active").removeClass("active");
                $(this).addClass("active");
                $(".navbar-small-buttons").slideUp(300, function(){
                    _omplusFunctions.changeManagementPage(helper);
                });
            }
        }else if (this.smallButtons.length){
            var helper = this.omplusName;
            options.click = function(){
                $("#navbar .big-button button.active").removeClass("active");
                $(this).addClass("active");
                var nav = $(this).parent().parent().parent().next();
                if (nav.css("display") != "none"){
                    nav.slideUp(300);
                }
                else{
                    $(".navbar-small-buttons").each(function(){
                        if ($(this).attr("id") != nav.attr("id")){
                            $(this).slideUp(300);
                        }else{
                            $(this).slideDown(300);
                        }
                    });
                }
            }
        }
        Omplus.base.create(Omplus.views.Button, options);
        this.components[0].components.push(this.omplusName + "_big");
        for(var i in this.smallButtons){
            var small = $.extend(true, {}, {
                omplusType:"Button",
                cssClasses:["small-button"],
                omplusName:this.omplusName + "_small_" + i,
                width: this.smallWidth,
                height: this.smallHeight,
                controller : "languageController",
                model : this.smallButtons[i].model,
                management: this.smallButtons[i]["management"],
                init : function(){
                    var helper = this;
                    this.click = function(){
                        $(".navbar-small-buttons").slideUp(300, function(){
                            _omplusFunctions.changeManagementPage(helper.management);
                        });
                    }
                }
            });
            Omplus.base.create(Omplus.views.Button, small);
            this.components[1].components.push(this.omplusName + "_small_" + i);
        }
    },
    layout:{
        omplusType:"VerticalLayout"
    },
    mainButton :{
        model:null,
        management: null
    },
    bigWidth: "140px",
    bigHeight: "64px",
    smallWidth: "140px",
    smallHeight: "25px",
    smallButtons:[],
    components:[
        {
            omplusType:"Panel",
            components:[]
        },
        {
            omplusType:"Panel",
            cssClasses:["navbar-small-buttons"],
            components:[],
            layout:{
                omplusType:"HorizontalLayout"
            }
        }
    ]
});

Omplus.views.Navbar = Omplus.base.extend(Omplus.views.Panel, {
    omplusType:"Navbar",
    init : function(){
        for(var i in this.buttons){
            this.buttons[i].omplusName = this.omplusName + "_" + i;
            Omplus.base.create(Omplus.views.NavbarButton, this.buttons[i]);
            this.components.push(this.omplusName + "_" + i);
        }
    },
    layout:{
        omplusType:"HorizontalLayout"
    },
    buttons:[],
    bigWidth: "150px",
    bigHeight: "700px",
    smallWidth: "50px",
    smallHeight: "50px",
    components:[]
});